<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace oss_cs\driver;

/**
 * Description of QcCosApi
 *
 * @author SoChishun
 */
class QcloudCOS {

    protected $client;

    public function __construct() {
        $this->client = new \Qcloud\Cos\Client(array('region' => config('sdkaccountsu.cos_region'),
            'credentials' => array(
                'appId'=> config('sdkaccountsu.lvb_appid'),
                'secretId' => config('sdkaccountsu.api_secretId'),
                'secretKey' => config('sdkaccountsu.api_SecretKey'))));
        ;
    }

    /**
     * 
     * @param type $bucket bucket的命名规则为{name}-{appid} ，此处填写的存储桶名称必须为此格式
     * @param type $object
     * @param type $file
     * @return array array('Expiration'=>'','ETag'=>'"bb58a573107bbf5adafa4b9c577deaa0"','ServerSideEncryption'=>'','VersionId'=>'','SSECustomerAlgorithm'=>'','SSECustomerKeyMD5'=>'','SSEKMSKeyId'=>'','RequestCharged'=>'','RequestId'=>'NWFkMjAxNGVfY2NhMzNiMGFfNDc0YV8yY2E3NzM=','Location'=>'http://izb-gz-1251232469.cos.ap-guangzhou.myqcloud.com/public%2Faabbcc.txt',)
     */
    public function uploadFile($bucket, $object, $file) {
        try {
            $result = $this->client->upload($bucket, $object, fopen($file, 'r+'));
            return $result->toArray();
        } catch (\Exception $e) {
            echo "$e\n";
            return $e->getMessage();
        }
    }

}
